package com.ibeetl.admin.aod.service;

import com.ibeetl.admin.aod.dao.AodBlackListDao;
import com.ibeetl.admin.aod.entity.AodBlackList;
import com.ibeetl.admin.aod.entity.AodIndustry;
import com.ibeetl.admin.aod.entity.AodIndustryData;
import com.ibeetl.admin.aod.util.ToolUtil;
import com.ibeetl.admin.aod.web.query.AodBlackListQuery;
import com.ibeetl.admin.aod.web.query.AodIndustryDataQuery;
import com.ibeetl.admin.aod.web.query.AodIndustryQuery;
import com.ibeetl.admin.core.service.BaseService;
import com.ibeetl.admin.core.util.PlatformException;
import org.apache.commons.collections.CollectionUtils;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * AodBlackList Service
 */

@Service
@Transactional
public class AodBlackListService extends BaseService<AodBlackList>{

    @Autowired
    protected AodBlackListDao aodBlackListDao;

    @Autowired
    @Qualifier("defaultJdbcTemplate")
    protected JdbcTemplate jdbcTemplate;

    @Autowired
    protected AodIndustryService aodIndustryService;

    public PageQuery<AodBlackList>queryByCondition(PageQuery query){
        PageQuery ret =  aodBlackListDao.queryByCondition(query);
        queryListAfter(ret.getList());
        return ret;
    }

    public void batchDelAodBlackList(List<Long> ids){
        try {
            aodBlackListDao.batchDelAodBlackListByIds(ids);
        } catch (Exception e) {
            throw new PlatformException("批量删除黑名单失败", e);
        }
    }

    /**
     * @param aodBlackList
     */
    public void saveAndDeleteIndustryData(AodBlackList aodBlackList) {

        AodBlackList existAodBlackList = aodBlackListDao.queryByPhoneNumber(aodBlackList.getPhoneNumber());
        if (null != existAodBlackList) {
            throw new PlatformException(String.format("黑名单【%s】已存在", aodBlackList.getPhoneNumber()));
        }

        aodBlackListDao.insert(aodBlackList);

        List<AodIndustry> aodIndustryList = aodIndustryService.queryList(new AodIndustryQuery());
        String deleteSql = "delete from %s where phone_number = '%s' ";
        if (CollectionUtils.isNotEmpty(aodIndustryList)) {
            for (AodIndustry aodIndustry : aodIndustryList) {
                // 删除行业数据表中与黑名单匹配的数据
                String industryTableName = aodIndustry.getIndustryTable();
                jdbcTemplate.execute(String.format(deleteSql, industryTableName, aodBlackList.getPhoneNumber()));
            }
        }
    }

    public List<String> queryAllPhoneNumber(AodBlackListQuery aodBlackListQuery) {
        return aodBlackListDao.queryAllPhoneNumber(aodBlackListQuery);
    }

    public void updateAndDeleteIndustryData(AodBlackList aodBlackList) {

        AodBlackList existAodBlackList = aodBlackListDao.queryByPhoneNumberNonId(aodBlackList);
        if (null != existAodBlackList) {
            throw new PlatformException(String.format("黑名单【%s】已存在", aodBlackList.getPhoneNumber()));
        }

        update(aodBlackList);

        List<AodIndustry> aodIndustryList = aodIndustryService.queryList(new AodIndustryQuery());
        String deleteSql = "delete from %s where phone_number = '%s' ";
        if (CollectionUtils.isNotEmpty(aodIndustryList)) {
            for (AodIndustry aodIndustry : aodIndustryList) {
                // 删除行业数据表中与黑名单匹配的数据
                String industryTableName = aodIndustry.getIndustryTable();
                jdbcTemplate.execute(String.format(deleteSql, industryTableName, aodBlackList.getPhoneNumber()));
            }
        }
    }

}